package com.aigou.admin.controller;

import com.aigou.admin.entity.User;
import com.aigou.admin.service.UserService;
import com.aigou.admin.service.impl.UserServiceImpl;
import com.alibaba.fastjson2.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 查询一个用户的servlet
 */
@WebServlet("/findUser")
public class FindUserController extends HttpServlet {

    //调用service
    private UserService userService = new UserServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获得用户id
        String userId = req.getParameter("userId");
        //查询用户
        User user = userService.findById(userId);
        //转换为JSON,通过流发送给客户端
        resp.getWriter().write(JSON.toJSONString(user));
    }
}